﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PetkaWebDav.Server.Framework.BaseClasses;
using Microsoft.WindowsAzure.Storage.Blob;

namespace PetkaWebDav.Handler.AzureBlob.Handlers
{
    /// <summary>
    /// Custom implementation example for DavUnlock.
    /// </summary>
    public sealed class DavUnlock : DavUnlockBase
    {
        public DavUnlock()
        {
            this.ProcessDavRequest += new DavProcessEventHandler(DavUnlock_ProcessDavRequest);
        }

        private void DavUnlock_ProcessDavRequest(object sender, EventArgs e)
        {
            //Check to see if a lock already exists
            CloudBlockBlob _lockFile = RequestWrapper.GetLockFileByLockToken(base.LockToken);

            if (_lockFile == null)
                base.AbortRequest(DavUnlockResponseCode.PreconditionFailed);
            else
            {
                RequestWrapper.DeleteAzureFile(_lockFile.Name);
            }
        }
    }
}
